<template>
  <div v-if="isRouterAlive" class="components-fof">
    <div class="fof-con">
      <div class="left-fof">
        <el-menu ref="asideMenu" :default-active="defaultActive" :unique-opened="true" class="el-menu-vertical-demo">
          <template v-for="(item, key) in fundDataList">
            <!-- 父级菜单 -->
            <el-submenu v-if="item.children && item.children.length > 0" :key="key" :item="item" :index="item.id" @click="handleNodeClick(item)">
              <template slot="title">
                <span> {{ $t(item.menuName) }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item v-for="(val, index) in item.children" :key="index" :item="val" :index="val.id" @click="handleNodeClick(val)">
                <template slot="title">
                  <span> {{ $t(val.menuName) }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
            <!-- 没有子节点 -->
            <el-menu-item v-else :key="key" :item="item" :index="item.id"><svg-icon :icon-class="item.icon" />{{ $t(item.menuName) }}</el-menu-item>

          </template>
        </el-menu>
        <div class="add-fof">
          <el-button style="color: #0C0C0C" class="el-icon-plus" type="text" @click="addHandle">{{ $t('添加母基金') }}</el-button>
          <el-button style="color: #0C0C0C" class="el-icon-delete" type="text" @click="delHandle">{{ $t('删除母基金') }}</el-button>
        </div>
      </div>
      <!-- 内容区域 -->
      <div class="right-fof">
        <div v-show="!(menuPath === undefined || menuPath === null || menuPath === '')">
          <component :is="menuPath" ref="menuPath" />
        </div>
      </div>
    </div>
    <!-- 基金基本信息 -->
    <el-dialog :title="$t('母基金基本信息')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <fundBaseForm ref="fundBaseForm" :fund-data-type="'fof'" />
    </el-dialog>
    <!-- 基金基本信息 -->
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog v-el-drag-dialog :title="$t('删除母基金')" :close-on-click-modal="false" :visible.sync="delFofOpen" width="400px">
      <el-form ref="form" :model="delFofForm" :rules="rules">
        <el-form-item :label="$t('母基金名称')" prop="fofFundId">
          <el-select v-model="delFofForm.fofFundId" size="small" style="width: 100%" clearable :placeholder="$t('请选择')">
            <el-option
              v-for="dict in fundDataList"
              :key="dict.id"
              :value="dict.id"
              :label="$t(dict.fundName)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="deleteHandle">{{ $t("删除") }}</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import fundBaseForm from '@/views/fund/base/fundBaseForm.vue'
import { getRoutersByMenuKey } from '@/api/system/menu'
import { getAsyncComponent } from '@/utils'
import { delFundBase, getFundBaseFofList } from '@/api/fund/fundBase'

export default {
  components: {
    fundBaseForm
  },
  props: {
  },
  data() {
    return {
      formOpen: false,
      delFofOpen: false,
      // 菜单列表
      childMenuTabs: [],
      delFofForm: {
        fofFundId: undefined
      },
      childMenuTabsOther: [],
      menuPath: undefined,
      queryParams: {
        fundName: undefined,
        pageNum: 1,
        pageSize: 100
      },
      rules: {},
      fundDataList: [],
      defaultActive: '',
      isRouterAlive: true
    }
  },
  watch: {
    '$route': {
      handler(nval, oval) {
        console.log(nval, oval)
        if (nval.fullPath === oval.fullPath) {
          this.isRouterAlive = false
          this.$nextTick(function() {
            this.isRouterAlive = true
            this.listLoad()
          })
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.listLoad()
  },
  mounted() {
  },
  beforeDestroy() {
  },
  methods: {
    /** 获取菜单 */
    listLoad() {
      // 菜单列表
      getRoutersByMenuKey('fof_menu_list', 'C').then(response => {
        this.childMenuTabs = response.rows
      })
      getRoutersByMenuKey('fof_other_menu', 'C').then(response => {
        this.childMenuTabsOther = response.rows
        this.getList()
      })
    },
    /** 获取列表 */
    getList() {
      getFundBaseFofList(this.queryParams).then(response => {
        if (response.rows !== null) {
          this.fundDataList = response.rows
          this.fundDataList.forEach((item) => {
            if (item.menuKey !== 'fof_other_menu') {
              item.menuName = item.fundName
              item.fundId = item.id
              var children = JSON.parse(JSON.stringify(this.childMenuTabs))
              this.setFundId(children, item.id)
              item.children = children
            }
          })
          // 基金以外的固有菜单
          if (this.childMenuTabsOther) {
            this.childMenuTabsOther.forEach(item => {
              this.fundDataList.push(item)
            })
          }
          if (this.childMenuTabs !== null) {
            var tag = this.fundDataList[0].children[0]
            this.handleNodeClick(tag)
          }
        } else {
          this.msgWarning(this.$t('请先添加基金'))
        }
      })
    },
    // 递归设置菜单的基金ID
    setFundId(menuList, fundId) {
      if (menuList !== undefined && menuList !== null) {
        menuList.forEach(item => {
          item.fundId = fundId
          this.setFundId(item.children, fundId)
        })
      }
    },
    // 点击基金或菜单事件
    handleNodeClick(data) {
      this.$nextTick(() => {
        if (data.component && data.fundId) {
          this.importFormPath(data.component, data.menuKey, data.fundId)
        } else {
          this.importFormPath(data.component, data.menuKey, undefined)
        }
      })
    },
    // 引入组件
    importFormPath(menuPath, menuKey, fundId) {
      if (menuPath) {
        var parameter = {}
        if (menuPath.indexOf('?') === -1) { // 不含问号
          this.menuPath = getAsyncComponent('@/views/' + menuPath)
          parameter = {}
        } else {
          const index = menuPath.lastIndexOf('?')
          const sMenuPath = menuPath.substring(0, index)
          this.menuPath = getAsyncComponent('@/views/' + sMenuPath)

          // 增加参数
          const newKey = menuPath.split('?')[1]
          var newstr = newKey.replace(/=/g, '":"')
          const objectKey = '{"' + newstr.replace(/&/g, '","') + '"}'
          parameter = JSON.parse(objectKey)
        }
        // 地址栏添加左侧菜单地址
        this.$nextTick(() => {
          if (fundId !== undefined && fundId !== null && fundId !== '') {
            this.$route.query.id = fundId
            this.$route.query.fundType = 'fof'
            Object.keys(parameter).forEach(key => {
              this.$route.query[key] = parameter[key]
            })
          }
        })
        this.gotoSubMethod(menuKey, fundId)
      } else {
        const error = 'error/404'
        this.importFormPath(error)
      }
    },
    gotoSubMethod(menuKey, fundId) {
      this.$nextTick(() => {
        if (menuKey === 'fof_direct_investment_project') {
          this.$route.query.searchKey = 'fof_deal'
          if (fundId !== undefined && fundId !== null && fundId !== '') {
            this.$route.query.paramsMap = { investorId: fundId }
          }
          // this.$bus.$once('async_done', () => {
          //   this.$refs.menuPath && this.$refs.menuPath.init({ id: fundId })
          // })
        }
        if (menuKey === 'fof_investment_sub_fund') {
          this.$route.query.searchKey = 'fof_fund'
          if (fundId !== undefined && fundId !== null && fundId !== '') {
            this.$route.query.paramsMap = { investorId: fundId }
          }
        }
        if (menuKey === 'fof_indirect_items') {
          this.$route.query.searchKey = 'fof_intermediate'
          if (fundId !== undefined && fundId !== null && fundId !== '') {
            this.$route.query.paramsMap = { investorId: fundId }
          }
        }
      })
    },
    /** 添加母基金 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.fundBaseForm.init({ fundStatus: undefined })
      })
    },
    /** 删除母基金 */
    delHandle() {
      this.delFofOpen = true
    },
    // 取消按钮
    cancel() {
      this.delFofOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.delFofForm = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 确定删除fof基金 */
    deleteHandle() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          var id = this.delFofForm.fofFundId
          this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
          }).then(function() {
            console.log('删除的基金ID', id)
            return delFundBase(id)
          }).then(() => {
            this.delFofForm.fofFundId = undefined
            this.delFofOpen = false
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
          }).catch(function() {
          })
        } else {
          this.locationError(document)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.components-fof{
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  .fof-con{
    display: flex;
    height: 100%;
  }
  .left-fof{
    width: 200px;
    height: 100%;
    background: #fff;
    .el-menu{
      height: calc(100% - 45px);
      // overflow: auto;
      overflow-y: auto !important;
      .svg-icon {
        margin-right: 16px;
      }
      .el-menu-item:hover, .el-menu-item:focus {
        outline: none;
        background-color: transparent;
      }
    }
    .add-fof{
      border-top: 1px solid #ececec;
      text-align: center;
      line-height: 45px;
      cursor: pointer;
    }
  }
  .right-fof{
    flex: 1;
    overflow: auto;
    padding: 15px;
    // margin-right: -15px;
  }
}
</style>

